package com.management.vtribe.controller;

import com.management.vtribe.Bean.Category;
import com.management.vtribe.config.Result;
import com.management.vtribe.config.Tool;
import com.management.vtribe.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @RequestMapping("/getAllCategory")
    public List<Category> getAllCategory(){
        return categoryService.getAllCategory();
    }

    @RequestMapping("/getSomeCategory")
    public List<Category> getSomeCategory(int page,int num){
        return categoryService.getSomeCategory(page, num);
    }

    @RequestMapping("/getCountOfCategory")
    public int getCountOfCategory(){
        return categoryService.getCountOfCategory();
    }

    @RequestMapping("/insertCategory")
    public  Result insertCategory(String name){
        if(name==null||name.equals("")){return new Result(400,"新增失败！");}
        int res = categoryService.insertCategory(name);
        if(res!=0){
            return new Result(200,"新增成功！");
        }else{
            return new Result(400,"栏目名已存在！");
        }
    }

    @RequestMapping("/updateCategoryById")
    public Result updateCategoryById(int id,String name){
        if(name==null||name.equals("")){return new Result(400,"更新失败！");}
        int res = categoryService.updateCategoryById(id,name);
        if(res!=0){
            return new Result(200,"更新成功！");
        }else{
            return new Result(400,"栏目名已存在,您可以通过新增的方式启用该栏目！");
        }
    }

    @RequestMapping("/deleteCategoryById")
    public Result deleteCategoryById(String cIds){
        if(cIds==null||cIds.equals("")){return new Result(400,"删除失败！");}
        int[] ids = new Tool().toIntArray(cIds);
        int res = categoryService.deleteCategoryById(ids);
        if(res!=0){
            return new Result(200,"删除成功！");
        }else{
            return new Result(400,"删除失败！");
        }
    }
}
